<?php

namespace app\api\controller;

use app\admin\model\Cartype;
use app\admin\model\Servicetype;
use app\admin\model\store\Usercoupon;
use app\admin\model\Unlocktype;
use app\api\model\Goods;
use app\api\model\JoinMerchant;
use app\api\model\JoinOnboarding;
use app\api\model\Order;
use app\common\controller\Api;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
//    protected $noNeedLogin = ['*'];
//    protected $noNeedRight = ['*'];

    private  $user_token = '';

    private  $array_action=[];
    public function _initialize(Request $request = null)
    {
//        parent::__construct($request);
//        var_dump();die;

        //用户登录token
        $action=Request::instance()->action();

        $token=Request::instance()->header('token');
        $this->user_token=json_decode(base64_decode($token),1);
//        $this->order_taker_id=JoinOnboarding::where(['user_id'=>$this->user_token['user_id']])->value('id');
        if(!in_array($action,$this->array_action)){

            if(!$this->user_token){

                $this->success("未传token");

            }
        }
//
    }
    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }


    /**
     * 电动车出租列表
     * index/index/list_of_rental_electric_vehicles
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public function  list_of_rental_electric_vehicles(Request $request)
    {
          $post=$request->post();
          $where['product_type']=2;
          if(isset($post['search'])){
              if($post['search']){
                  $where['name']=['like','%'.$post['search'].'%'];
              }
          }
        if(isset($post['join_merchant_id'])){
            if($post['join_merchant_id']){
                $where['join_merchant_id']=$post['join_merchant_id'];
            }
        }
        $data= Goods::where($where)
               ->field('id,name,gallery_images,day_money,moth_money,frame_number,license_plate_number,cz_label,deposit')
               ->page($post['page'],$post['pagesize'])
               ->select();
          if($data){
              $data=yuming(json_array($data),'gallery_images');
              foreach ($data as &$value){
                  $value['cz_label']=explode(',',$value['cz_label']);
              }

          }
          return json(['code'=>200,'mag'=>'请求成功','data'=>$data]);

    }

    /**
     * 电动车出租详情 支付前页面
     * index/index/details_of_electric_vehicle_rental
     * @param Request $request
     * @return void
     */
    public   function  details_of_electric_vehicle_rental(Request $request)
    {
        $post=$request->post();
        $data['goods']=Goods::where(['id'=>$post['goods_id']])->field('id,name,gallery_images,day_money,moth_money,frame_number,license_plate_number,cz_label,deposit,join_merchant_id')->find();
        $data['goods']['gallery_images']=yuming($data['goods']['gallery_images']);
        $data['goods']['cz_label']=explode(',',$data['goods']['cz_label']);
        $data['address']=JoinMerchant::where(['id'=>$data['goods']['join_merchant_id']])->field('id,trade_name,address')->find();
        $data['coupon']=json_array(Usercoupon::where(['user_id'=>$this->auth->id,'money'=>['egt',$post['money']]])->select());
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);

    }


    public function  electric_vehicle_rental_payment(Request $request)
    {
        $post=$request->post();

        $data=[
             //订单类型
             'product_type'=>2,
             //租期
             'lease_term'=>$post['lease_term'],
             //客户地址
             'address'=>$post['address'],
             //客户详情地址
             'detailed_address'=>$post['detailed_address'],
             //收货人手机号
             'mobile'=>$post['mobile'],
             //优惠券id
            'usercoupon_id'=>$post['usercoupon_id'],
            //押金金额
            'deposit'=>$post['deposit'],
            //优惠金额
            'coupon_price'=>$post['coupon_price'],
            //订单费用
            'order_price'=>$post['order_price'],
            //实付费用
            'actual_price'=>$post['actual_price'],
            //身份证正面
            'id_card_front_image'=>$post['id_card_front_image'],
            //身份证反面
            'id_card_back_image'=>$post['id_card_back_image'],
            //下单用户
            'user_id'=>$this->user_token['user_id'],
            //商家id
            'store_id'=>$post['join_merchant_id'],
            //下单时间
            'createtime'=>time(),
        ];

        if($post['lease_type']==1){
            $data['zstart_time']=time();
            $data['zend_time']=time()+(86400*$post['lease_term']);
        }
        if($post['lease_type']==2){
            $data['zstart_time']=time();
            $data['zend_time']=time()+((86400*30)*$post['lease_term']);
        }
        $post['lease_type']==1?$data['lease_type']='日租':$data['lease_type']='月租';
//        var_dump($data);die;
        $insert=Order::create($data);
        if($insert){
           return json(['code'=>200,'msg'=>'已下单']);
        }
    }




    /**
     * 商家列表
     * index/index/store_list
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public function  store_list(Request $request)
    {
         $post=$request->post();
         $data=JoinMerchant::where(['status'=>2])
             ->field('id,trade_name,address,business_status,start,end,
             (6378.138 * 2 * asin(sqrt(pow(sin((lat * pi() / 180 - '.$post['lat'].' * pi() / 180) / 2),2) + cos('.$post['lat'].' * pi() / 180) * cos(lat * pi() / 180) * pow(sin((lng * pi() / 180 - '.$post['lng'].' * pi() / 180) / 2),2))) * 1000) as distance')
             ->order("distance desc")
             ->page($post['page'],$post['pagesize'])
             ->select();

         return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
    }


    /**
     * 车型列表
     * index/list_of_vehicle_models
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public function list_of_vehicle_models(Request $request)
    {
        $post=$request->post();

        $Cartype= Cartype::where(['switch'=>1])->field('id,title')->select();

        return  json(['code'=>200,'msg'=>'请求成功','data'=>$Cartype]);

    }

    /**
     * 拖车下单
     * index/trailer_ordering
     * @param Request $request
     * @return \think\response\Json|void
     *
     */
    public  function trailer_ordering(Request $request)
    {
        $post=$request->post();
          $data=[
                //订单类型
               'product_type'=>4,
               //车型
               'defective_vehicle_model'=>$post['defective_vehicle_model'],
               //现场图片
               'on_site_photos'=>$post['on_site_photos'],
              //目的地
              'trailer_address'=>$post['trailer_address'],
              //目的地经度
              'trailer_longitude'=>$post['trailer_longitude'],
              //目的地纬度
              'trailer_latitude'=>$post['trailer_latitude'],
              //车辆地址
              'user_location'=>$post['user_location'],
              //车辆经度
              'user_longitude'=>$post['user_longitude'],
              //车辆纬度
              'user_latitude'=>$post['user_latitude'],
              //姓名
              'consignee'=>$post['consignee'],
              //手机号
              'mobile'=>$post['mobile'],
              //用户优惠券id
              'usercoupon_id'=>$post['usercoupon_id'],
              //优惠金额
              'coupon_price'=>$post['coupon_price'],
              //订单费用
              'order_price'=>$post['order_price'],
              //实付费用
              'actual_price'=>$post['actual_price'],
              //下单用户
              'user_id'=>$this->user_token['user_id'],
              //下单时间
              'createtime'=>time(),
          ];
        $insert=Order::create($data);
        if($insert){
            return json(['code'=>200,'msg'=>'已下单']);
        }
    }

    /**
     * 开锁类型
     * index/unlocktype
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public function unlocktype(Request $request)
    {
        $post=$request->post();

        $Cartype= Unlocktype::where(['switch'=>1])->field('id,title')->select();

        return  json(['code'=>200,'msg'=>'请求成功','data'=>$Cartype]);

    }

    /**
     * 开锁下单
     * index/unlock_and_place_an_order
     * @param Request $request
     * @return \think\response\Json|void
     *
     */
    public  function unlock_and_place_an_order(Request $request)
    {
        $post=$request->post();
        $data=[
            //订单类型
            'product_type'=>6,
            //开锁类型
            'unlock_type'=>$post['unlock_type'],
            //现场图片
            'on_site_photos'=>$post['on_site_photos'],
            //服务地址
            'user_location'=>$post['user_location'],
            //服务地址经度
            'user_longitude'=>$post['user_longitude'],
            //服务地址纬度
            'user_latitude'=>$post['user_latitude'],
            //姓名
            'consignee'=>$post['consignee'],
            //手机号
            'mobile'=>$post['mobile'],
            //实付费用
            'actual_price'=>$post['actual_price'],
            //下单用户
            'user_id'=>$this->user_token['user_id'],
            //下单时间
            'createtime'=>time(),
        ];
        $insert=Order::create($data);
        if($insert){
            return json(['code'=>200,'msg'=>'已下单']);
        }
    }

    /**
     * 服务类型
     * index/servicetype
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public function servicetype(Request $request)
    {
        $post=$request->post();

        $Servicetype= Servicetype::field('id,title')->select();

        return  json(['code'=>200,'msg'=>'请求成功','data'=>$Servicetype]);

    }


    /**
     * 服务下单
     * index/service_ordering
     * @param Request $request
     * @return \think\response\Json|void
     *
     */
    public  function service_ordering(Request $request)
    {
        $post=$request->post();
        $data=[
            //订单类型
            'product_type'=>5,
            //服务类型
            'servicetype'=>$post['servicetype'],
            //服务类型id
            'servicetype_id'=>$post['servicetype_id'],
            //现场图片
            'on_site_photos'=>$post['on_site_photos'],
            //描述
            'on_site_content'=>$post['on_site_content'],
            //服务地址
            'user_location'=>$post['user_location'],
            //服务地址经度
            'user_longitude'=>$post['user_longitude'],
            //服务地址纬度
            'user_latitude'=>$post['user_latitude'],
            //姓名
            'consignee'=>$post['consignee'],
            //手机号
            'mobile'=>$post['mobile'],
            //服务时间
            'service_time'=>$post['service_time'],
            //是否加急
            'service_j'=>$post['service_j'],
            //用户优惠券id
            'usercoupon_id'=>$post['usercoupon_id'],
            //优惠金额
            'coupon_price'=>$post['coupon_price'],
            //订单费用
            'order_price'=>$post['order_price'],
            //实付费用
            'actual_price'=>$post['actual_price'],
            //下单用户
            'user_id'=>$this->user_token['user_id'],
            //下单时间
            'createtime'=>time(),
        ];
        if($post['servicetype_id']==1){
           $data['service_num']=$post['service_num'];
        }
        if($post['servicetype_id']==2){
            $data['budget_quotation']=$post['budget_quotation'];
        }
        $insert=Order::create($data);
        if($insert){
            return json(['code'=>200,'msg'=>'已下单']);
        }
    }
}
